import org.junit.experimental.max.MaxHistory;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

public class StringDemo {
    public static void main(String[] args) {

        String str = "2147483648";
        int k = -2147483648;
//        String x = zconvert(str,k);
        String strs[] = new String[]{"fsdfsafedfdgethfhjhkgyyhdssdsdrefsfrgtrehfgdfvdrvfcv","fsdfsafedfdgethfhjhkgyyhdssdsdref","fsdfsafedfdgethfhjhkgyyhdssdsdref"};
//        String x = longestCommonPrefix(strs);
//        ArrayList<Integer> x = splitString(str);
//        int x = reverse(k);
//        int x = StrToInt(str);
        String s = "11",t = "99";
        String x = solve(s,t);
        System.out.println("x = " + x);
    }

    private static String solve(String s, String t) {
        return null;
    }

    private static int StrToInt(String s) {
        int result = 0;
        int unsignedFlag = 1; // 符号
        boolean flag = true;
        for (int i =  0; i < s.length(); i++) {
            if(s.charAt(i)==' ' && flag) continue;
            else{
                if(flag){
                    // 第一个字符,为符号
                    flag = false;
                    if(s.charAt(i) == '-') {
                        unsignedFlag = -unsignedFlag;
                        continue;
                    }
                }
                // 第一个字符
                if(s.charAt(i) >='0' && s.charAt(i)<='9'){
                    long multipy = result*10;
                    long res = multipy+(s.charAt(i)-'0');
                    System.out.println("multipy = " + multipy);
                    System.out.println("res = " + res);
                    System.out.println("unsignedFlag*result = " + (unsignedFlag*res));
                    if(unsignedFlag*res > Math.pow(2,31)-1 ||  10*unsignedFlag*res > Math.pow(2,31)-1)return (int) (Math.pow(2,31)-1);
                    else if(unsignedFlag*res<Math.pow(-2,31) || 10*unsignedFlag*res<Math.pow(-2,31)) return (int)(Math.pow(-2,31));
                    result = (int)res;
                    System.out.println("Math.pow(2,31)-1 = " + (Math.pow(2, 31) - 1));
                }else return unsignedFlag*result;
            }
        }
        return unsignedFlag*result;
    }

    private static int reverse(int x) {
        String str = Math.abs(x)+"";
        String val = "";
        System.out.println("str = " + str);
        if(x > Math.pow(2,31)-1 || x< Math.pow(-2,31)) return 0;
        for (int i = str.length()-1; i >= 0; i--) {
            val += str.charAt(i);
        }
        int result = Integer.parseInt(val);
        return x>0?result:-result;
    }

    private static ArrayList<Integer> splitString(String str) {
        ArrayList<Integer> result = new ArrayList<>();
        if(str.length()==0) return null;
        else if(str.length() == 1) {result.add(1);return result;}
        int alpha[] = new int[26];
        for (int i = 0; i < str.length(); i++) {
            alpha[str.charAt(i)-'a'] = i;
        }
        for (int i = 0; i < alpha.length; i++) {
            System.out.print(alpha[i]+" ");
        }
        int l = 0,r = 0;
        for (int i = 0; i < str.length(); i++) {
            r = Math.max(alpha[str.charAt(i)-'a'],r);
            if(i == r) {
                result.add(r-l+1);
                l = r+1;
                r = 0;
            }
        }
        return result;

    }

    private static String longestCommonPrefix(String[] strs) {
        if (strs.length <=0) return null;
        String result = strs[0];
        int len = Integer.MAX_VALUE;
        for (int i = 0; i < strs.length; i++) {
            String str = strs[i];
            len = Math.min(len,str.length());
            String res = "";
            System.out.println("i = " + i);
            System.out.println(result.length()+"len = " + len);
            for (int j = 0; j < len; j++) {
                System.out.print("j = " + j);
                System.out.print("result.charAt(j) = " + result.charAt(j));
                System.out.println("result = " + result);
                if(result.charAt(j) == str.charAt(j)){
                    res += result.charAt(j);
                }
            }
            result = res;
        }
        return result;
    }

    private static String zconvert(String str, int k) {
        int n = 0;
        if(str.length()<=k) n = 1;
        else n = str.length()-1-k;
        char [][]value = new char[k][n];
        for (int i = 0,t = 0; t < str.length(); i++) {
            // 从上到下
            for (int j = 0; j < (k); j++) {
                value[j][i] = str.charAt(t++);
                getValue(value);
            }
            // 中间
            System.out.println("i = " + i);
            System.out.println("t = " + t);
            if(t>=str.length()) break;

            value[k/2][++i] = str.charAt(t++);
        }
        String x = getValue(value);
        return x;
    }
    private static String getValue(char res[][]){
        String result = "";
        for (int i = 0; i < res.length; i++) {
            for (int j = 0; j < res[i].length; j++) {
                result += res[i][j];
                System.out.print(i+" "+j+" "+res[i][j]);
            }
            System.out.println();
        }
        System.out.println();
        return result;
    }
}
